package com.example.octopus.controller;

import com.alibaba.fastjson2.JSON;
import com.example.octopus.entity.Comment;
import com.example.octopus.entity.Product;
import com.example.octopus.entity.SendComment;
import com.example.octopus.entity.returnMessage.DeleteReturnMessage;
import com.example.octopus.entity.returnMessage.InsertReturnMessage;
import com.example.octopus.entity.returnMessage.SelectReturnMessage;
import com.example.octopus.entity.returnMessage.UpdateReturnMessage;
import com.example.octopus.entity.returnMessage.SendSelectReturnMessage;
import com.example.octopus.entity.returnMessage.SendReturnMessage;
import com.example.octopus.service.CommentService;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.gson.Gson;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.*;
@RestController
@RequestMapping("api/comment/comment")
public class CommentController {
    @Autowired
    CommentService commentService;
    @Autowired
    Gson gson;
    private static final String TrueNumber ="0";
    private static final int TureReturnCode =200;
    private static final int ErrReturnCode =500;
    //等待yml注入
    String env="cloud1-6gma1k2s0ff0ac6b";
    @PostMapping
    public SendReturnMessage createComment(@RequestBody Comment comment){
        // 生成一个随机UUID字符串并去除连字符作为数据库表主键_id
        String randomString = UUID.randomUUID().toString().replaceAll("-", "");
        if (randomString.length() != 32) {
            System.out.println("生成字符串长度不正确，应该为32位。");
        } else {
            // 输出生成的32位随机字符串
            System.out.println(randomString);
        }
        System.out.println(comment);
        if(comment.getCommentId()==null){
            comment.set_id(randomString);
            comment.setCommentId(randomString);
            //检测是否有重复id，有就拒绝插入
            HashMap<String,Object> condition = new HashMap<>();
            condition.put("commentId",comment.getCommentId());
            System.out.println(condition);
            System.out.println(comment);
                ArrayList<Comment> comments=new ArrayList<>();
                comments.add(comment);
                //数据库插入返回
                InsertReturnMessage insertReturnMessage=commentService.insert(env,comments);
                if(insertReturnMessage.getErrcode().equals(TrueNumber)){
                    return new SendReturnMessage(TureReturnCode,insertReturnMessage.getErrmsg());
                }else{
                    return new SendReturnMessage(ErrReturnCode,insertReturnMessage.getErrmsg());
                }
        }
        return new SendReturnMessage(ErrReturnCode,"无效id");
    }
    @GetMapping("/{productId}/pages/{page}")
    public SendSelectReturnMessage getCommentByProductId(@PathVariable String productId,@PathVariable int page) {
        Map<String,Object> conditions=new HashMap<>();
        conditions.put("productId",productId);
        SelectReturnMessage selectReturnMessage=commentService.select(env,conditions,page);
        if(selectReturnMessage.getErrcode().equals(TrueNumber)){
            List<SendComment> sendComments=new ArrayList<>();
            for(String s: selectReturnMessage.getData()){
                SendComment sendComment= JSON.parseObject(s, SendComment.class);
                sendComments.add(sendComment);
            }
            return new SendSelectReturnMessage(TureReturnCode,selectReturnMessage.getErrmsg(),sendComments);
        }else{
            return new SendSelectReturnMessage(TureReturnCode,selectReturnMessage.getErrmsg(),null);
        }
    }
    @DeleteMapping("/{commentId}")
    public SendReturnMessage deleteComment(@PathVariable String commentId){
        Map<String,Object> conditions=new HashMap<>();
        conditions.put("commentId",commentId);
        DeleteReturnMessage deleteReturnMessage=commentService.delete(env,conditions);
        if(deleteReturnMessage.getErrcode().equals(TrueNumber)){
            return new SendReturnMessage(TureReturnCode,deleteReturnMessage.getErrmsg());
        }else {
            return new SendReturnMessage(ErrReturnCode,deleteReturnMessage.getErrmsg());
        }
    }
}
